﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XCommunity.Documents;
using XCommunity.Web;
using XCommunity;
using XCommunity.Frames;
using XCommunity.Forums;
using XCommunity.Forums.Views;

public partial class Forums_LaunchTopic : System.Web.UI.Page
{
  protected void Page_Load( object sender, EventArgs e )
  {

  }


  public ForumsSystem Forums
  {
    get { return WebCommunityContext.Current.ForumsSystem; }

  }


  protected void OnLaunchTopic( object sender, EventArgs e )
  {

    if ( CaptionTextBox.Text.Trim() == "" )
    {
      Response.Write( "麻烦写两个字做标题，点击浏览器的后退继续" );
      Response.End();
    }



    AccountManage.RedirectLogin();

    //    if ( AccountContext.CurrentAccount == null )
    //      Response.Redirect( "Login.aspx", true );



    string route = Request.QueryString["route"];

    string boardId = Request.QueryString["board"];

    ForumsBoardFrame board = Forums.GetBoard( boardId );

    if ( board == null )
      throw new InvalidOperationException();//UNDONE 异常的详细信息


    IHtmlDocumentProvider documentPlugin = WebCommunityContext.Current.GetDefaultDocumentProvider();

    Dictionary<string, string> meta = new Dictionary<string, string>() { { "Caption", CaptionTextBox.Text } };
    Document document = documentPlugin.CreateDocument( ContentTextBox.Text, meta );

    ICommunityContext context = WebCommunityContext.CurrentCommunityContext;

    context.DocumentService.SaveDocument( document );

    int topicId = board.LauchTopic( document ).TopicId;

    Response.Redirect( "FrameView.aspx?route=" + HttpUtility.UrlEncode( route + "/#" + topicId ) );

  }
}
